<?php

require_once(BASE_PATH . '/framework/libraries/wechat.class.php');

class wxSdk extends Wechat
{
    private $path;
    private $authorised;//授权方式

    public function __construct()
    {
        $options['token'] = "";
        $options['appid'] = "wxc68b4e72af13c1cf";
        $options['appsecret'] = "d9c3554d06110ad3920422e90cbd1e09";
        $options['logcallback'] = "recordLog";
        if (empty($options['authorised']) || !in_array($options['authorised'], array('snsapi_userinfo', 'snsapi_base'))) {
            $this->authorised = 'snsapi_base';
        } else {
            $this->authorised = $options['authorised'];
        }
        parent::__construct($options);
        $this->path = BASE_PATH . '/data/';
    }



    public function test()
    {
        //$this->setCache('asttttt','444444444',1500);
        //return $this->getCache('asttttt');
        //$this->removeCache('asttttt');
        //unlink('D:/phpStudy/WWW/shopnc/weixin/data/asttttt.json');
    }


    /**
     * webAuthGetCode
     * 微信网页授权第一步 ，获取code
     */
    public function webAuthGetCode()
    {
        global $config;
        $requestUrl = $config['site_url']."{$_SERVER['REQUEST_URI']}";//SITE_URL . $_SERVER['REQUEST_URI'];
        $oauthUrl = $this->getOauthRedirect($requestUrl, 'oauth', $this->authorised);
        header('Location:' . $oauthUrl);
        exit;
    }


    /**
     * log overwrite
     * @see Wechat::log()
     */
    protected function log($log)
    {
        if ($this->debug) {
            if (function_exists($this->logcallback)) {
                if (is_array($log)) $log = serialize($log);
                return call_user_func($this->logcallback, $log);
            }
            return false;
//            elseif (class_exists('Log')) {
//                Log::write('wechat：' . $log, Log::DEBUG);
//                return true;
//            }
        }
        return false;
    }

    /**
     * 重载设置缓存
     * @param string $cachename
     * @param mixed $value
     * @param int $expired
     * @return boolean
     */
    protected function setCache($cachename, $value, $expired)
    {
        $data['jsapi_ticket'] = $value;
        $data['expire_time'] = time() + intval($expired);
        file_put_contents("{$this->path}$cachename.json", json_encode($data));
        return true;
    }

    /**
     * 重载获取缓存
     * @param string $cachename
     * @return mixed
     */
    protected function getCache($cachename)
    {
        if (file_exists("{$this->path}$cachename.json")) {
            $data = json_decode(file_get_contents("{$this->path}$cachename.json"));
            if ($data->expire_time < time()) {
                return false;
            } else {
                return $data->jsapi_ticket;
            }
        } else {
            return false;
        }
    }

    /**
     * 重载清除缓存
     * @param string $cachename
     * @return boolean
     */
    protected function removeCache($cachename)
    {
        if (file_exists("{$this->path}$cachename.json")) {
            if (unlink("{$this->path}$cachename.json")) return true;
            return false;
        }
        return true;
    }

    /**
     * recordLog
     * 记录日志
     * $log string
     */
    private function recordLog($log = '')
    {
        $now = date('Y-m-d H:i:s', time());
        $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
        $content = "[{$now}]: {$log}\r\n";
        if (file_put_contents($log_file, $content, FILE_APPEND)) return true;
        return false;
    }
}